﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace Terraria.Audio
{
	// Token: 0x0200049B RID: 1179
	public class ActiveSound
	{
		// Token: 0x170002CB RID: 715
		// (get) Token: 0x06002780 RID: 10112 RVA: 0x0001B9B4 File Offset: 0x00019BB4
		public SoundEffectInstance Sound
		{
			get
			{
				return this._sound;
			}
		}

		// Token: 0x170002CC RID: 716
		// (get) Token: 0x06002781 RID: 10113 RVA: 0x0001B9BC File Offset: 0x00019BBC
		public SoundStyle Style
		{
			get
			{
				return this._style;
			}
		}

		// Token: 0x170002CD RID: 717
		// (get) Token: 0x06002782 RID: 10114 RVA: 0x0001B9C4 File Offset: 0x00019BC4
		public bool IsPlaying
		{
			get
			{
				return this.Sound.State == SoundState.Playing;
			}
		}

		// Token: 0x06002783 RID: 10115 RVA: 0x0001B9D4 File Offset: 0x00019BD4
		public ActiveSound(SoundStyle style, Vector2 position)
		{
			this.Position = position;
			this.Volume = 1f;
			this.IsGlobal = false;
			this._style = style;
			this.Play();
		}

		// Token: 0x06002784 RID: 10116 RVA: 0x0001BA02 File Offset: 0x00019C02
		public ActiveSound(SoundStyle style)
		{
			this.Position = Vector2.Zero;
			this.Volume = 1f;
			this.IsGlobal = true;
			this._style = style;
			this.Play();
		}

		// Token: 0x06002785 RID: 10117 RVA: 0x00494BD8 File Offset: 0x00492DD8
		private void Play()
		{
			SoundEffectInstance soundEffectInstance = this._style.GetRandomSound().CreateInstance();
			soundEffectInstance.Pitch += this._style.GetRandomPitch();
			Main.PlaySoundInstance(soundEffectInstance);
			this._sound = soundEffectInstance;
			this.Update();
		}

		// Token: 0x06002786 RID: 10118 RVA: 0x0001BA34 File Offset: 0x00019C34
		public void Stop()
		{
			if (this._sound != null)
			{
				this._sound.Stop();
			}
		}

		// Token: 0x06002787 RID: 10119 RVA: 0x0001BA49 File Offset: 0x00019C49
		public void Pause()
		{
			if (this._sound != null && this._sound.State == SoundState.Playing)
			{
				this._sound.Pause();
			}
		}

		// Token: 0x06002788 RID: 10120 RVA: 0x0001BA6B File Offset: 0x00019C6B
		public void Resume()
		{
			if (this._sound != null && this._sound.State == SoundState.Paused)
			{
				this._sound.Resume();
			}
		}

		// Token: 0x06002789 RID: 10121 RVA: 0x00494C24 File Offset: 0x00492E24
		public void Update()
		{
			if (this._sound == null)
			{
				return;
			}
			Vector2 vector = Main.screenPosition + new Vector2((float)(Main.screenWidth / 2), (float)(Main.screenHeight / 2));
			float num = 1f;
			if (!this.IsGlobal)
			{
				float num2 = (this.Position.X - vector.X) / ((float)Main.screenWidth * 0.5f);
				num2 = MathHelper.Clamp(num2, -1f, 1f);
				this.Sound.Pan = num2;
				float num3 = Vector2.Distance(this.Position, vector);
				num = 1f - num3 / ((float)Main.screenWidth * 1.5f);
			}
			num *= this._style.Volume * this.Volume;
			switch (this._style.Type)
			{
			case SoundType.Sound:
				num *= Main.soundVolume;
				break;
			case SoundType.Ambient:
				num *= Main.ambientVolume;
				break;
			case SoundType.Music:
				num *= Main.musicVolume;
				break;
			}
			num = MathHelper.Clamp(num, 0f, 1f);
			this.Sound.Volume = num;
		}

		// Token: 0x040043C7 RID: 17351
		private SoundEffectInstance _sound;

		// Token: 0x040043C8 RID: 17352
		public readonly bool IsGlobal;

		// Token: 0x040043C9 RID: 17353
		public Vector2 Position;

		// Token: 0x040043CA RID: 17354
		public float Volume;

		// Token: 0x040043CB RID: 17355
		private SoundStyle _style;
	}
}
